using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPages_manager : CMSMasterPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            DataBind();
    }

    public override void DataBind()
    {
        DropDownList ddlManageWebsite = lvNavigation.FindControl("ddlManageWebsite") as DropDownList;
        if (ddlManageWebsite != null)
        {
            ddlManageWebsite.Items.Clear();
            ddlManageWebsite.DataSource = TanashCMS.Business.TanashWebsite.List((int)Profile.GetPropertyValue("CustomerID"));
        }

        base.DataBind();

        if (ddlManageWebsite == null) return;

        ddlManageWebsite.Items.Insert(0, new ListItem("Select...", ""));
        ddlManageWebsite.SelectedIndex = 0;
    }

    public override void setPublishLinkVisibility(bool Visible)
    {
        lvNavigation.FindControl("linkPublish").Visible = Visible;
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (Page.Website != null)
        {
            if (Page.Website.NeedsPublish)
                lvNavigation.FindControl("linkPublish").Visible = true;
            else
                lvNavigation.FindControl("linkPublish").Visible = false;
        }

        base.Render(writer);
    }
    protected void Menu1_Init(object sender, EventArgs e)
    {
        Menu menuManagerNavigation = lvNavigation.FindControl("menuManagerNavigation") as Menu;
        if (menuManagerNavigation == null) return;

        if (Page.Website.getOptionValue("IsBlogSite") != "1")
            menuManagerNavigation.Items.RemoveAt(5);
        
        if (!Page.User.IsInRole("administrator"))
        {
            menuManagerNavigation.Items.RemoveAt(1); // customers
            menuManagerNavigation.Items.RemoveAt(1); // websites
        }
    }

    protected void ddlManageWebsite_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlManageWebsite = lvNavigation.FindControl("ddlManageWebsite") as DropDownList;
        if (ddlManageWebsite == null) return;
        if (ddlManageWebsite.SelectedIndex == 0) return;
        Profile.SetPropertyValue("WebsiteID", int.Parse(ddlManageWebsite.SelectedValue));
        Profile.Save();
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnChangeWebsite_Click(object sender, EventArgs e)
    {
        DropDownList ddlManageWebsite = lvNavigation.FindControl("ddlManageWebsite") as DropDownList;
        if (ddlManageWebsite == null) return;
        if (ddlManageWebsite.SelectedIndex == 0) return;
        Profile.SetPropertyValue("WebsiteID", int.Parse(ddlManageWebsite.SelectedValue));
        Profile.Save();
        Response.Redirect(Request.RawUrl, true);        
    }

    protected void odsWebsites_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        DropDownList ddlManageWebsite = lvNavigation.FindControl("ddlManageWebsite") as DropDownList;
        if (ddlManageWebsite == null) return;
        ddlManageWebsite.Items.Clear();
        ddlManageWebsite.Items.Add(new ListItem("Select...", ""));
        if (Page.User.IsInRole("administrator"))
        {
            e.InputParameters["CustomerID"] = 0;
        }
    }
    protected void ddlManageWebsite_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlManageWebsite = lvNavigation.FindControl("ddlManageWebsite") as DropDownList;
        Panel panelSelectWebsite = lvNavigation.FindControl("panelSelectWebsite") as Panel;
        if (ddlManageWebsite == null) return;
        if (ddlManageWebsite.Items.FindByValue(Profile.GetPropertyValue("WebsiteID").ToString()) != null)
            ddlManageWebsite.SelectedIndex = ddlManageWebsite.Items.IndexOf(ddlManageWebsite.Items.FindByValue(Profile.GetPropertyValue("WebsiteID").ToString()));

        if (ddlManageWebsite.Items.Count <= 2)
            panelSelectWebsite.Visible = false;
    }
}
